package com.xique.park.mq.receiver;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.xique.park.mq.bean.ParkInMsg;
import com.xique.park.mq.constants.TopicConstants;
import com.xique.park.mq.consumer.ParkInFinishMqConsumer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

/**
 * @author shd
 * @create 2022/7/29 10:37
 */
@Slf4j
@Component
@RequiredArgsConstructor
@RocketMQMessageListener(topic = TopicConstants.PARK_IN_FINISH_TOPIC, consumerGroup = TopicConstants.PARK_IN_FINISH_GROUP, consumeMode = ConsumeMode.ORDERLY)
public class ParkInFinishRocketMqReceiver implements RocketMQListener<String> {

    private final ParkInFinishMqConsumer parkInFinishMqConsumer;

    @Override
    public void onMessage(String message) {
        try {
            log.info("[入场通知]-接收到入场通知：{}", message);
            if (StrUtil.isNotEmpty(message)) {
                ParkInMsg parkInMsg = JSON.parseObject(message, ParkInMsg.class);
                parkInFinishMqConsumer.receive(parkInMsg);
            } else {
                log.warn("[入场通知]-入场消息为空");
            }
        } catch (JSONException e) {
            e.printStackTrace();
            log.error("[入场通知]-消息转JSON异常，{}", message);
        }
    }

}
